<template>
  <div class="con">
    <el-form
      ref="dataForm"
      :model="temp"
      :rules="rules"
      label-position="left"
      label-width="150px"

    >
      <el-form-item label="今日厂商总收益" prop="money">
        <el-input v-model.trim="temp.money" placeholder="请输入今日厂商总收益" />
      </el-form-item>
      <el-form-item label="单价" prop="price">
        <el-input v-model.trim="temp.price" placeholder="请输入广告单价" />
      </el-form-item>
      <el-form-item label="cnzz_cookie" prop="cnzz_cookie">
        <el-input
          type="textarea"
          v-model.trim="temp.cnzz_cookie"
          placeholder="请输入cnzz登陆cookie"
        />
      </el-form-item>
      <div style="text-align: left">
        <el-button type="primary" @click="updateData()"> 确定修改 </el-button>
        <span style="font-size: 12px;
    color: #999;padding-top:10px">厂商收益和单价只填一个就可以，如果两个都填了，默认用单价计算</span>
      </div>
    </el-form>
    <div style="margin-top: 30px;">
      <el-button type="primary" @click="jisuan()" :loading="loading">生成结账账单</el-button><br/>
      <span style="font-size: 12px;
    color: #999;padding-top:10px">生成结账账单需要5～10分钟，轻耐心等待</span>
    </div>
    
  </div>
</template>
<script>
import { get_cnzz_config, update_cnzz_config, get_cnzz_data} from "../../api/cnzz/dashboard";
import { MessageBox, Message } from "element-ui";
export default {
  data() {
    return {
      temp: {
        money: "",
        cnzz_cookie: "",
        price : ""
      },
      loading: false,
      rules: {
        money: [
          {
            validator: (rule, value, callback) => {
              if (/^[1-9][0-9]*(\.[0-9]{1,2})?$/.test(value) === false && value) {
                callback(new Error("请输入数字，只支持两位小数"));
              } else if(!value && !this.temp.price) {
                callback(new Error("总收益和单价必填一个"));
              } else{
                callback();
              }
            },
            trigger: "blur",
          },
        ],
         price: [
          {
            validator: (rule, value, callback) => {
              if (/^[0-9][0-9]*(\.[0-9]{1,2})?$/.test(value) === false && value) {
                callback(new Error("请输入数字，只支持两位小数"));
              } else if(!value && !this.temp.money) {
                callback(new Error("总收益和单价必填一个"));
              } else{
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        cnzz_cookie: [
          { required: true, message: "请输入cnzz登陆cookie", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      get_cnzz_config().then((res) => {
        this.temp = res.DATA;
      });
    },
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          update_cnzz_config(this.temp).then((res) => {
            if (res.STATUS == 200) {
              Message({
                message: "修改成功",
                type: "success",
                duration: 2000,
              });
            } else {
              Message({
                message: res.DEC,
                type: "success",
                duration: 2000,
              });
            }
          });
        }
      });
    },
    jisuan() {
      MessageBox("去生成账单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
            this.loading = true
            get_cnzz_data().then(res => {
                if (res.STATUS == 200){
                    Message({
                        type: "success",
                        message: "生成成功!",
                    });
                    this.loading = false
                }else{
                    this.loading = false
                }
            }).catch(res => {
                
            })
          
        })
        .catch(() => {});
    },
  },
};
</script>
<style>
.con {
  padding: 20px;
}
</style>